#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>

#include "user.h"
#include "customer.h"

int mainMenu()
{
	int c ;
	showTime(1);
	printf("\t\t\t         旅店管理系统\n");
	printf("\t\t\t=============================\n");
	printf("\t\t\t      1: 用户登录\n");
	printf("\t\t\t      2: 游客登陆\n");
	printf("\t\t\t      0: 注销\n");
	printf("\t\t\t=============================\n");
	printf("\t\t\t请选择:");
	scanf("%d",&c);
	while(c < 0||c > 2)
	{
		printf("请重新输入:");
		scanf("%d%*c",&c);
	}
	return c ;

}
void showTime(int k)
{
	struct tm *ptr;
	time_t it;
	time(&it);
	ptr = localtime(&it);
	if (k == 1 )
	{
		if (ptr->tm_hour >= 5 && ptr->tm_hour <= 8 )
			printf("\t\t\t早上好!\n");
		else if (ptr->tm_hour >= 8 && ptr->tm_hour <= 11 )
			printf("\t\t\t上午好!\n");
		else if (ptr->tm_hour >= 12 && ptr->tm_hour <= 13 )
			printf("\t\t\t中午好!\n");
		else if (ptr->tm_hour >= 14 && ptr->tm_hour <= 17 )
			printf("\t\t\t下午好!\n");
		else if (ptr->tm_hour >= 18 && ptr->tm_hour <= 24 )
			printf("\t\t\t晚上好!\n");
		printf("\t\t\t现在是 %4d 年 %02d 月 %02d 日 %02d:%02d:%02d\n",ptr->tm_year+1900,ptr->tm_mon+1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec);
		printf("\t\t\t欢迎光临!\n\n");
	}
	else
	{
		if (ptr->tm_hour >= 5 && ptr->tm_hour <= 8 )
			printf("\t\t\t早上好!\n");
		else if (ptr->tm_hour >= 8 && ptr->tm_hour <= 11 )
			printf("\t\t\t上午好!\n");
		else if (ptr->tm_hour >= 12 && ptr->tm_hour <= 13 )
			printf("\t\t\t中午好!\n");
		else if (ptr->tm_hour >= 14 && ptr->tm_hour <= 17 )
			printf("\t\t\t下午好!\n");
		else if (ptr->tm_hour >= 18 && ptr->tm_hour <= 24 )
			printf("\t\t\t晚上好!\n");
		printf("\t\t\t现在是 %4d 年 %02d 月 %02d 日 %02d:%02d:%02d\n",ptr->tm_year+1900,ptr->tm_mon+1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec);
		printf("\t\t\t欢迎下次光临!\n\n");
	}

		
}

User *login_user()
{
	char userId[12];
	char password[7];
	int count = 0 ;	//try count;
	User *head,*pr;

	if( access("user.txt",F_OK) != 0 )
	{
		showTime(1);
		printf("系统无储存信息，请输入管理猿ID和密码:\n");
		printf("=============================================\n");
		printf("请输入管理猿猴ID :\n");
		scanf("%s",userId);
		while(strcmp(userId,"admin0007") != 0)
		{
			printf("管理猿ID错误，请重新输入，目前剩余次数:%d :\n",2-count);
			scanf("%s",userId);
			if(count++ > 2) return NULL;
		}
		printf("=============================================\n");
		printf("请输入管理员密码: \n");
		count = 0;
		scanf("%s",password);
		while(strcmp(password,"123456") != 0)	
		{
			printf("密码输入错误，请重新输入，目前剩余次数:%d :\n" ,2-count);
			scanf("%s",password);
			if(count++ > 2) return NULL;
		}	

		pr = (User*)malloc(sizeof(User));
		if(pr == NULL)
		{
			return NULL;
		}
		memset(pr,0,sizeof(User));
		strcpy(pr->userId,userId);
		strcpy(pr->password,password);
		strcpy(pr->role,"admin");
		getchar(); //read empty;
		printf("请输入管理猿名字:\n");
		fgets(pr->name,8,stdin);
		getchar();
		printf("请输入管理猿邮箱:\n");
		fgets(pr->email,19,stdin);
		printf("请输入管理猿性别(m/f)");
		fgets(pr->sex,4,stdin);

		write_user(pr);
		return pr ;
	}

	else
	{
		head = read_user();
		pr = head;
		count = 0;

		printf("请输入用户ID :\n");
		scanf("%s",userId);
		getchar();
		printf("请输入用户密码: \n");
		scanf("%s",password);
		getchar();

		while(1)
		{
			while(pr->next)
			{
				if(!strcmp(pr->userId,userId))
					break;
				pr = pr->next;
			}
			if(!pr->next)
			{
				if(2-count == 0)
				{
					printf("账号密码输入错误3次，自动返回\n");
					return NULL;
				}
				printf("无该用户ID，请重新输入,目前剩余次数:%d :\n" ,2-count);
				count++;
				printf("请输入用户ID :\n");
				scanf("%s",userId);
				getchar();
				printf("请输入用户密码: \n");
				scanf("%s",password);
				getchar();
				continue;
			}
			if(strcmp(pr->password,password))
			{
				if(2-count == 0)
				{
					printf("账号密码输入错误3次，自动返回\n");
					return NULL;
				}
				printf("密码错误，请重新输入,目前剩余次数:%d :\n" ,2-count);
				count++;
				printf("请输入用户ID :\n");
				scanf("%s",userId);
				getchar();
				printf("请输入用户密码: \n");
				scanf("%s",password);
				getchar();
				continue;
			}
			else
				return pr;
		}


//		pr = pr->next;
//		pr = pr->next;
//		pr = pr->next;
		//display_user(pr);

//		return pr;
	}
}

void enterSystem(User *r)
{
	if(strcmp(r->role,"admin") == 0)
		adminSystem(r);
	if(strcmp(r->role,"manager") == 0)
		managerSystem(r);
	if(strcmp(r->role,"recept") == 0)
		receptSystem(r);
	if(strcmp(r->role,"cleaner") == 0)
		cleanerSystem(r);
	
}

customer *login_customer()
{
	char userId[12];
	char password[7];
	int count = 0 ;	//try count;
	customer *pr;
	if( access("customer.txt",F_OK) != 0 )
	{
		showTime(1);
		printf("旅客无储存信息，请输入管理猿ID和密码:\n");
		printf("=============================================\n");
		printf("请输入管理猿ID :\n");
		scanf("%s",userId);
		while(strcmp(userId,"admin0007") != 0)
		{
			printf("管理猿ID错误，请重新输入，目前剩余次数:%d :\n",2-count);
			scanf("%s",userId);
			if(count++ > 2) return NULL;
		}
		printf("=============================================\n");
		printf("请输入管理员密码: \n");
		count = 0;
		scanf("%s",password);
		while(strcmp(password,"123456") != 0)	
		{
			printf("密码输入错误，请重新输入，目前剩余次数:%d :\n" ,2-count);
			scanf("%s",password);
			if(count++ > 2) return NULL;
		}

		pr = (customer*)malloc(sizeof(customer));
		if(pr == NULL)
		{
			return NULL;
		}
		memset(pr,0,sizeof(customer));
		printf("请输入房间号:\n");
		scanf("%d",&pr->roomId);
		getchar();
		printf("请输入顾客身份证号码:\n");
		fgets(pr->m_ID,19,stdin);
		getchar();
		printf("请输入顾客名字:\n");
		fgets(pr->m_name,8,stdin);
		printf("请输入顾客年龄:\n");
		scanf("%d",&pr->m_age);
		getchar();
		printf("请输入顾客性别(m/f):\n");
		fgets(pr->m_sex,4,stdin);
		printf("请输入顾客电话号码:\n");
		fgets(pr->phone,12,stdin);
		printf("请输入开房时间:\n");
		fgets(pr->inDay,12,stdin);
		printf("请输入退房时间:\n");
		fgets(pr->outDay,12,stdin);
		printf("请输入顾客状态:\n");
		scanf("%d",&pr->status);
		getchar();

		write_customer(pr);

		return pr;
	}	
	else 
	{	
		pr = read_customer();
		return pr;
	}
}	

int main(int argc,char const *argv[])
{
	User *r;
	customer *cus;
	int c,loop = 1;
	while(loop)
	{
		system("clear");
		c = mainMenu();
		switch(c)
		{
			case 1:		//user
				r = login_user();
				if (r == NULL)
					loop =0;
				else
					enterSystem(r);
				break;
			case 2:		//customer
				cus = login_customer();
				customerSystem(cus);
				break;	
			default:
				loop = 0;
				break;
		}
	}
}
